﻿@page "/User"
@using IdentityServer4Blazor.Shared.Dto.User;
@using AntDesign.TableModels
@inject ApiRequest.UserReuqest userRequest;

<Table @ref="table"
       DataSource="@userList"
       HidePagination="true"
       OnChange="@((QueryModel<UserListOutput> _)=> onChange(_))"
       OnPageIndexChange="async e=>await fetchData(e)"
       @bind-SelectedRows="selectedRows">
    <Selection Key="@(context.Id.ToString())" />
    <Column  Title="用户名" @bind-Field="@context.UserName" />
    <Column Title="昵称" @bind-Field="@context.NickName" Sortable />
    <Column Title="电话号码" @bind-Field="@context.PhoneNumber" Sortable />
    <Column Title="邮箱" @bind-Field="@context.Email" />
    <ActionColumn>
        <Space>
            <SpaceItem><Button Type="@ButtonType.Primary" OnClick="(x)=>GetClaims(x,context.Id)">Claims</Button></SpaceItem>
        </Space>
    </ActionColumn>
</Table>
<Pagination ShowSizeChanger="true" PageSizeOptions="new int[] {10,20,50,100 }" ShowQuickJumper="true" PageIndex="_pageIndex" Total="_total" PageSize="_pageSize" Style="margin-top:20px"></Pagination>
<Modal Title="claimsTitle"
       Visible="claimsModalVisible"
       OnOk="ClaimsModalHandleOk"
       OnCancel="ClaimsModalHandleOk" Style="width:50%">
    <Descriptions Bordered Style="width:auto">
        @foreach (var types in user.Claims.ToLookup(x=>x.Type))
        {
        <DescriptionsItem Title="@types.Key">
            @foreach (var item in types)
            {
                <p style="margin:0">
                    @item.Value <Icon Type="close" Class="deleteIcon" OnClick="(x)=>DeleteClaims(x,item.Id)"/> 
                </p>
            }
        </DescriptionsItem>
        }
    </Descriptions>
</Modal>
<Modal Title="@("删除Claim")"
       Visible="deleteClaimsModalVisible"
       OnOk="DeleteClaimsModalHandleOk"
       OnCancel="DeleteClaimsModalHandleCancle" 
       Style="width:200px">
    <h4>是否删除该声明?</h4>
</Modal>
@code {

    List<UserListOutput> userList;
    string claimsTitle;
    bool claimsModalVisible;
    bool deleteClaimsModalVisible;
    int deleteClaimId;
    IEnumerable<UserListOutput> selectedRows;
    ITable table;
    UserListOutput user;
    int _pageIndex = 1;
    int _pageSize = 10;
    int _total = 0;

    protected override async Task OnParametersSetAsync()
    {
        var res = await userRequest.GetUserList(_pageIndex, _pageSize);
        _total = res.Total;
        userList = res.Items;
    }

    async Task fetchData(PaginationEventArgs args)
    {
        var res = await userRequest.GetUserList(args.PageIndex, args.PageSize);
        _total = res.Total;
        userList = res.Items;
    }
    async Task GetClaims(MouseEventArgs eventArgs, int id)
    {
        user = userList.SingleOrDefault(x => x.Id.Equals(id));
        claimsTitle = $"{user.NickName}的Claims";
        claimsModalVisible = true;
    }
    async Task DeleteClaims(MouseEventArgs eventArgs, int id)
    {
        deleteClaimId = id;
        deleteClaimsModalVisible = true;
    }
    void ClaimsModalHandleOk(MouseEventArgs eventArgs)
    {
        claimsModalVisible = false;
    }
    async Task DeleteClaimsModalHandleOk(MouseEventArgs eventArgs)
    {
        user.Claims.Remove(user.Claims.SingleOrDefault(x => x.Id.Equals(deleteClaimId)));
        deleteClaimsModalVisible = false;
        await _message.Success("删除成功！");
    }
    void DeleteClaimsModalHandleCancle(MouseEventArgs eventArgs)
    {
        deleteClaimsModalVisible = false;
    }
    void onChange(QueryModel<UserListOutput> queryModel)
    {
        Console.WriteLine(queryModel);
    }

}
<style>
    .deleteIcon :hover{
        cursor:pointer;
    }
</style>
